package com.attendance.www.hr.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.attendance.www.hr.beans.MenuBean;
import com.attendance.www.hr.dao.mapper.EmployeeCorporationMapper;
import com.attendance.www.hr.dao.mapper.EmployeeMapper;
import com.attendance.www.hr.dao.mapper.MenuExtMapper;
import com.attendance.www.hr.dao.mapper.MenuMapper;
import com.attendance.www.hr.dao.mapper.SalaryRuleMapper;
import com.attendance.www.hr.entitys.Employee;
import com.attendance.www.hr.entitys.Menu;
import com.attendance.www.hr.entitys.MenuExample;
import com.attendance.www.hr.entitys.MenuExt;
import com.attendance.www.hr.entitys.SalaryRule;
import com.attendance.www.hr.entitys.SalaryRuleExample;
import com.attendance.www.hr.service.IMenuService;

@Service
public class MenuServiceImpl implements IMenuService{
	
	@Resource
	private MenuMapper menuDao;
	@Resource
	private MenuExtMapper menuExtDao;
	@Resource
	private EmployeeCorporationMapper employeeCorporationDao;
	@Resource
	private SalaryRuleMapper salaryRuleDao;
	@Resource
	private EmployeeMapper employeeDao;
	
	//遍历使用变量
	private List<MenuBean> menuBeanList=null;
	
	/**
	 * 根据URL匹配菜单，显示菜单名称
	 * @param url
	 * @return
	 */
	@Override
	public List<MenuBean> getMenuByUrl(String url){
		List<MenuBean> menuBeanList = new ArrayList<MenuBean>();
		MenuExample example=new MenuExample();
		example.createCriteria().andUrlLike("%"+url+"%");
		List<Menu> menuList=menuDao.selectByExample(example);
		MenuBean menuBean=null;
		if(menuList.size()>0){
			//三级菜单查询
			Menu menu=menuList.get(0);
			if(menu.getParentMenuId()!=null){
				Menu parentMenu2=menuDao.selectByPrimaryKey(menu.getParentMenuId());
				if(parentMenu2!=null){
					if(parentMenu2.getParentMenuId()!=null){
						Menu parentMenu1=menuDao.selectByPrimaryKey(parentMenu2.getParentMenuId());
						if(parentMenu1!=null){
							menuBean=new MenuBean();
							menuBean.setLabel(parentMenu1.getMenuName());
							menuBean.setIcons(parentMenu1.getIcons());
							menuBeanList.add(menuBean);
						}
					}
					menuBean=new MenuBean();
					menuBean.setLabel(parentMenu2.getMenuName());
					menuBean.setIcons(parentMenu2.getIcons());
					menuBeanList.add(menuBean);
				}
			}
			menuBean=new MenuBean();
			menuBean.setLabel(menu.getMenuName());
			menuBean.setIcons(menu.getIcons());
			menuBeanList.add(menuBean);
		}
		return menuBeanList;
	}
	
	/**
	 * 查询菜单数据
	 * @return
	 */
	@Override
	public List<MenuBean> getMenuList(){
		MenuExample example=new MenuExample();
		example.setOrderByClause("menu_id asc"); //设置排序
		List<Menu> menulist=menuDao.selectByExample(example);
		List<MenuBean> result = new ArrayList<MenuBean>();
		for(Menu menu : menulist){
			MenuBean menuBean = new MenuBean();
			menuBean.setId(menu.getMenuId());
			menuBean.setpId(menu.getParentMenuId());
			menuBean.setCode(menu.getMenuCode());
			menuBean.setLabel(menu.getMenuName());
			menuBean.setUrl(menu.getUrl());
			result.add(menuBean);
		}
		return result;
	}
  	
	/**
	 * 查询树菜单数据
	 * @param name
	 * @return
	 */
	@Override
	public List<MenuBean> getMenuTree(){
		menuBeanList=new ArrayList<MenuBean>();
		MenuExample example=new MenuExample();
		List<Menu> menuList=menuDao.selectByExample(example);
		List<MenuBean> menublist=new ArrayList<MenuBean>();
		if(menuList.size()>0){	
			for(Menu menu : menuList)
			{
				MenuBean menubean  = new MenuBean();
				menubean.setId(menu.getMenuId());
				menubean.setpId(menu.getParentMenuId());
				menubean.setLabel(menu.getMenuName());
				menubean.setUrl(menu.getUrl());
				menubean.setAlias(menu.getAlias());
				//menubean.setIsMainPage(menu.g);
				menublist.add(menubean);
			}
			menuBeanList=traversalMenu(menublist);
		}
		return menuBeanList;
	}
	
	/**
	 * 未登录，返回菜单URL
	 * @return
	 */
	@Override
	public List<MenuBean> getMenuIndex(){
		List<MenuBean> menuBeanList=new ArrayList<MenuBean>();
		MenuExample menuExample=new MenuExample();
		MenuExample.Criteria menuCriter=menuExample.createCriteria();
		menuCriter.andMenuCodeEqualTo("m_4");
		List<Menu> menuList=menuDao.selectByExample(menuExample);
		if(menuList.size()>0){
			MenuBean menuBean=new MenuBean();
			menuBean.setId(menuList.get(0).getMenuId());
			menuBean.setCode(menuList.get(0).getMenuCode());
			menuBean.setLabel(menuList.get(0).getMenuName());
			menuBean.setpId(0);
			menuBean.setUrl(menuList.get(0).getUrl());
			menuBeanList.add(menuBean);
		}
		return menuBeanList;
	}
	
	/**
	 * 用于登陆使用，根据角色ID查询菜单数据
	 * @param RoleId 角色ID
	 * @return
	 */
	@Override
	public List<MenuBean> getMenuTreeByLoginUser(Integer privilegeGroupId, Boolean isSalaryAuth) {
		menuBeanList=new ArrayList<MenuBean>();
		List<MenuExt> menuExtlist=new ArrayList<MenuExt>();
		if(privilegeGroupId!=null){
			//判断是否为HR权限
			/*if(privilegeGroupId.equals(4)){
				//查询员工是否有特殊分配公司
				EmployeeCorporationExample example=new EmployeeCorporationExample();
				example.createCriteria().andIsCorporationEqualTo(1);
				int record=employeeCorporationDao.countByExample(example);
				//如果存在分配公司，更改权限
				if(record>0){
					privilegeGroupId=5;
				}
			}*/
			menuExtlist=menuExtDao.selectByPrivilegeGroupId(privilegeGroupId,isSalaryAuth);
		}
		if(menuExtlist.size()>0){
			List<MenuBean> menubeanlist=new ArrayList<MenuBean>();	
			for(MenuExt menuExt : menuExtlist){
				MenuBean menubean  = new MenuBean();
				menubean.setId(menuExt.getMenuId());
				menubean.setpId(menuExt.getParentMenuId());
				menubean.setCode(menuExt.getMenuCode());
				menubean.setLabel(menuExt.getMenuName());
				menubean.setUrl(menuExt.getUrl());
				menubean.setAlias(menuExt.getAlias());
				menubean.setIsHomePage(menuExt.getIsHomePage());
				menubean.setIcons(menuExt.getIcons());
				menubeanlist.add(menubean);
			}
			menuBeanList=traversalMenu(menubeanlist);
		}
		return menuBeanList;
	}
	
	/**
	 * 设置菜单
	 * @param menuBeanlist List菜单数据源
	 * @return
	 */
	@Override
	@Transactional
	public boolean insertMenuTree(List<MenuBean> menuBeanlist){
		//删除菜单
		deleteMenu();
		//遍历循环设置数据
		recursiveMenuData(menuBeanlist,null);
		return true;
	}
	
	/**
     * 增加菜单
     * @param pId 序号
     * @param Name 菜单名称
     * @param Code 识别码
     * @param Url 地址
     * @return 返回值是ID
     */
	private Integer addMenu(String pId,String name,String code,String url,String alias){
		if(name!=null && code!=null){
			Menu menu=new Menu();
			menu.setParentMenuId(Integer.parseInt(pId));
			menu.setMenuName(name);
			menu.setMenuCode(code);
			menu.setUrl(url);
			menu.setAlias(alias);
			//menu.setIsMainpage(isMainpage);
			int record=menuDao.insertSelective(menu);
			if(record>0){
				return menu.getMenuId();
			}else{
				return record;
			}
		}else{
			return 0;
		}
	}
	
	/**
	 * 删除菜单
	 * @return
	 */
	private boolean deleteMenu(){
		MenuExample example=new MenuExample();
		int record=menuDao.deleteByExample(example);
		if(record>0){
			return true;
		}else{
			return false;
		}
	}
	
	/**
	 * 递归菜单数据源，设置菜单方法中调用
	 * @param menuBeanList List数据源
	 * @param pId 序号
	 */
	private void recursiveMenuData(List<MenuBean> menuBeanList,String pId)
	{
		for(MenuBean menubean : menuBeanList)
		{
			//增加菜单
			Integer id=addMenu(pId,menubean.getLabel(),menubean.getCode(),menubean.getUrl(),menubean.getAlias());
			
			//如果菜单节点存在下级菜单，递归数据
			List<MenuBean> menuChildren=menubean.getChildren();
			if(menuChildren.size()>0){
				if(id>0){
					//递归方法
					recursiveMenuData(menuChildren,id.toString());
				}
			}
		}
	}
	
	/**
	 * 遍历菜单，查询菜单数据方法调用，可共用方法
	 * @param menubList List数据源
	 * @return
	 */
	private List<MenuBean> traversalMenu(List<MenuBean> menubList)
	{
	     if (null == menubList){return menubList;}
	     for (MenuBean menubean : menubList)
	     {
	    	 //如果序号等于null，为最大级别
	    	 if(menubean.getpId()==null)
	    	 {
	    		 //增加菜单数据
	    		 menuBeanList.add(menubean);
	    		 //调用递归菜单数据
	    		 recursiveMenu(menubList,menubean.getId(),menubean);
	    	 }
	     }
	     return menuBeanList;
	}
	
	/**
	 * 递归菜单数据源，遍历菜单方法中调用
	 * @param menulist
	 * @param id
	 * @param menuBean
	 */
	private void recursiveMenu(List<MenuBean> menulist,Integer id,MenuBean menuBean)
	{
		 for (MenuBean menu : menulist)
		 {
			 if(menu.getpId()!=null){
//				 //获取菜单序号
//				 Integer pid=Integer.parseInt(menu.getpId());
				 if(id.equals(menu.getpId()))
				 {
					 //获取菜单子菜单数据
					 List<MenuBean> menuBeanlist = menuBean.getChildren();
					 //增加菜单
					 menuBeanlist.add(menu);
					 //设置子菜单数据
					 menuBean.setChildren(menuBeanlist);
					 //获取菜单ID
					 Integer mid=menu.getId();
					 //递归菜单
					 recursiveMenu(menulist,mid,menu);
				 }
			 }
		 }
	 }

	/**
	 * 获取是否有审批工资报表权限
	 */
	@Override
	public boolean getSalaryAuth(Integer employeeId) {
		// TODO Auto-generated method stub
		Employee emp = employeeDao.selectByPrimaryKey(employeeId);
		String employeeNo = emp.getEmployeeNo();
		SalaryRuleExample srExample = new SalaryRuleExample();
		srExample.or().andOneEmployeeNoEqualTo(employeeNo);
		srExample.or().andTwoEmployeeNoEqualTo(employeeNo);
		srExample.or().andThreeEmployeeNoEqualTo(employeeNo);
		srExample.or().andFourEmployeeNoEqualTo(employeeNo);
		srExample.or().andFiveEmployeeNoEqualTo(employeeNo);
		List<SalaryRule> srList = salaryRuleDao.selectByExample(srExample);
		if (srList.size()>0) {
			return true;
		}
		return false;
	}
}
